package com.njxzc.controller.admin;


import com.njxzc.common.http.ResponseResult;
import com.njxzc.common.security.anno.AuthIgrone;
import com.njxzc.entity.CartEntity;
import com.njxzc.entity.GoodsEntity;
import com.njxzc.service.admin.GoodsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/admin/goods")
public class GoodsController {
    @Autowired
    private GoodsServiceImpl goodsService;

    private  static String fileName;

    private static byte[] fileContent;

    @AuthIgrone
    @PostMapping("/fileInit")
    public void fileInit(@RequestBody MultipartFile file) {
        // MultipartFile对象不能在另一个方法中使用，所以把文件对象变成字节数组
        try {
            fileContent = file.getBytes();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        fileName = file.getOriginalFilename();
    }
    @PostMapping("/add")
    public ResponseResult<Map<String,String>> addGoods(@RequestBody GoodsEntity goodsEntity){
        goodsEntity.setFileName(fileName);
        goodsEntity.setLogoFile(fileContent);
        fileContent =new byte[0];


        return goodsService.addGoods(goodsEntity);
    }

    @AuthIgrone
    @PostMapping("/getGoods")
    public ResponseResult<Map<String,Object>> getGoods(@RequestBody GoodsEntity goodsEntity){
        return goodsService.getGoods(goodsEntity);
    }
    @AuthIgrone
    @PostMapping("/getAdvGoods")
    public ResponseResult<List<GoodsEntity>>getAdvGoods(){
        return goodsService.getAdvGoods();
    }
    @AuthIgrone
    @PostMapping("/getGoodsById")
    public ResponseResult<GoodsEntity>getGoodsById(@RequestBody GoodsEntity goodsEntity){
        return goodsService.getGoodsById(goodsEntity);
    }
    @AuthIgrone
    @PostMapping("/getGoodsIndex")
    public ResponseResult<List<GoodsEntity>>getGoodsIndex(@RequestBody GoodsEntity goodsEntity){
        return goodsService.getGoodsIndex(goodsEntity);
    }

    @AuthIgrone
    @PostMapping("/getGoodsByTYpe")
    public ResponseResult<List<GoodsEntity>>getGoodsByType(@RequestBody GoodsEntity goodsEntity){
        return null;
    }
    @AuthIgrone
    @PostMapping("/deleteGoods")
    public  ResponseResult<List<GoodsEntity>> deleteGoods(@RequestBody GoodsEntity goodsEntity){
        return goodsService.deleteGoods(goodsEntity);
    }
    public ResponseResult<List<Map<String,Object>>>  myCartGoods(@RequestBody CartEntity cartEntity){
        return goodsService.myCarGoods(cartEntity);
    }

}
